package com.smartlib.cmnObject.util;

import android.util.Log;


/***
 * log工具类
 */
public class LogUtil
{
    private static final String TAG = "project";
    private static final boolean DEBUG = true;
    private static final boolean DETAIL_ENABLE = true;


    public static void logV(String msg)
    {
        if (DEBUG && msg != null)
        {
            Log.v(TAG, buildMsg(msg));
        }
    }


    public static void logD(String msg)
    {
        if (DEBUG && msg != null)
        {
            Log.i(TAG, buildMsg(msg));
        }
    }

    public static void logI(String msg)
    {
        if (msg != null)
        {
            Log.i(TAG, buildMsg(msg));
        }
    }

    public static void logW(String msg)
    {
        if (msg != null)
        {
            Log.w(TAG, buildMsg(msg));
        }
    }

    public static void logE(String msg)
    {
        if (msg != null)
        {
            Log.e(TAG, buildMsg(msg));
        }
    }

    private static String buildMsg(String msg)
    {
        StringBuilder buffer = new StringBuilder();

        if (DETAIL_ENABLE)
        {
            final StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
            StackTraceElement stackTraceElement = null;
            for (int index = 4; index < stackTraceElements.length; index++)
            {
                stackTraceElement = stackTraceElements[index];
                if (!stackTraceElement.getMethodName().startsWith("log"))
                {
                    break;
                }
            }

            if (stackTraceElement != null)
            {
                buffer.append("[ ");
                buffer.append(Thread.currentThread().getName());
                buffer.append(": ");
                buffer.append(stackTraceElement.getFileName());
                buffer.append(": ");
                buffer.append(stackTraceElement.getLineNumber());
                buffer.append(": ");
                buffer.append(stackTraceElement.getMethodName());

                buffer.append("() ] ");
            }
        }

        buffer.append(msg);

        return buffer.toString();
    }


}
